<template>
  <el-card class="box-card" style="width: 1180px">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="sel">
        <el-form-item>
          <el-input
            v-model="sel.roleName"
            placeholder="角色名称"
            :clearable="true"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadRoles"
            ><i class="el-icon-search"></i
          ></el-button>
          <el-button type="success" @click="dialog.addshow = true"
            >新建</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="roleList" style="width: 100%">
      <el-table-column prop="id" label="角色编号" width="400px">
      </el-table-column>
      <el-table-column prop="roleName" label="角色名称" width="400px">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-popconfirm title="这是一段内容确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
          <el-button
            type="danger"
            size="mini"
            @click="assignPermission(scope.row.id)"
            >角色分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加角色" :visible.sync="dialog.addshow" width="32%">
      <el-form ref="addForm" :model="addForm" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="addForm.roleName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.addshow = false">取 消</el-button>
        <el-button type="primary" @click="save()">提 交</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改角色" :visible.sync="dialog.updateshow" width="32%">
            <el-form ref="updateForm" :model="updateForm" label-width="80px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="角色名称" prop="roleName">
                            <el-input v-model="updateForm.roleName"></el-input>
                        </el-form-item>

                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.updateshow = false">取 消</el-button>
                <el-button type="primary" @click="update()">提 交</el-button>
            </span>
        </el-dialog>


  </el-card>
</template>
  
  <script>
export default {
  data() {
    return {
      roleList: [],
      addForm: {},
      updateForm: {},
      dialog: {
        addshow: false,
        updateshow: false,
      },
      sel: {
        roleName: "",
      },
    };
  },
  methods: {
    save() {
            this.$axios.post("/roles/add/", this.addForm).then(res => {
                if (res.data.code = 200) {
                    this.dialog.addshow = false;
                    this.$message.success("添加成功~");
                    this.addForm = {};
                    this.loadRoles();
                }
            })
        },
        update() {
            this.$axios.put("/roles", this.updateForm)
                .then(res => {
                    if (res.data.code = 200) {
                        this.dialog.updateshow = false;
                        this.$message.success("修改成功~");
                        this.loadRoles();
                    }

                })
        },
        handleEdit(id) {
            this.dialog.updateshow = true;
            var aa = {id}
            this.$axios.post("/roles/selOne",aa).then(res => {
                this.updateForm = res.data.data.list;
            }).catch(error => {
                this.$message.error("出错了，稍后重试")
            });
        },
        handleDelete(id) {
            this.$axios.delete("/roles/" + id).then(res => {
                if (res.data.code != 200) {
                    this.$message.error(res.data.message);
                    this.loadRoles();
                }else{
                    this.$message.success("删除成功~");
                    this.loadRoles();
                }
            })
        },
    assignPermission(rid) {
      this.$router.push({ path: "/Permission", query: { rid: rid } });
    },
    loadRoles() {
      this.$axios.get("/roles").then((res) => {
        this.roleList = res.data.data.roleList;
      });
    },
  },
  mounted() {
    this.loadRoles();
  },
};
</script>